package com.dtp.core.notify.filter;

import com.dtp.core.support.ExecutorWrapper;
import com.dtp.common.entity.NotifyItem;
import com.dtp.common.pattern.filter.Invoker;
import com.dtp.core.context.BaseNotifyCtx;
import lombok.extern.slf4j.Slf4j;
import lombok.val;
import org.apache.commons.collections.CollectionUtils;

import java.util.Objects;

/**
 * 基础通知过滤器
 *
 * @author yanhom
 * @since 1.1.0
 *
 * @remark muse
 **/
@Slf4j
public class NoticeBaseFilter implements NotifyFilter {

    /** 执行过滤操作 */
    @Override
    public void doFilter(BaseNotifyCtx context, Invoker<BaseNotifyCtx> nextInvoker) {
        // 步骤1：消息是否满足发送条件，不满足则直接返回
        val executorWrapper = context.getExecutorWrapper();
        val notifyItem = context.getNotifyItem();
        if (Objects.isNull(notifyItem) || !satisfyBaseCondition(notifyItem, executorWrapper)) return;

        // 步骤2：调用Inovker实现类
        nextInvoker.invoke(context);
    }

    /** 判断是否满足消息发送 */
    private boolean satisfyBaseCondition(NotifyItem notifyItem, ExecutorWrapper executor) {
        return executor.isNotifyEnabled()                                   // 线程池上是否开启通知
                && notifyItem.isEnabled()                                   // 通知配置项上是否开启了通知
                && CollectionUtils.isNotEmpty(notifyItem.getPlatformIds()); // PlatformIds是否为空
    }

    @Override
    public int getOrder() {return 0;}
}
